PHP, MySQL, & JavaScript All-in-One For Dummies by Richard Blum

PHP, MySQL, & JavaScript All-in-One For Dummies by Richard Blum

Author:Richard Blum [Blum, Richard]
Language: eng
Format: epub
ISBN: 9781119468332
Publisher: Wiley
Published: 2018-04-24T00:00:00+00:00


Defining a class

Defining a class in PHP isn’t too different from defining a function. To define a new class, you use the class keyword, along with the name of the class, followed by any statements contained in the class.

Here's an example of a simple class definition:

class Product {

public $description;

public $price;

public $inventory;

public $onsale;

public function buyProduct($amount) {

$this->inventory -= $amount;

}

}

The class name you choose must be unique within your program. Class names follow the same rules as PHP variable names. Although it’s not required, programmers often start class names with an uppercase letter to help distinguish them in program code.

This example defines four property members and one method member. Each member is defined using one of three visibility classifications. The visibility of the member determines where you can use or reference that member. There are three visibility keywords used in PHP:

public: The member can be accessed from outside the class code.

private: The member can only be accessed from inside the class code.

protected: The member can only be accessed from a child class. (I talk about that a little later in the “Extending Classes” section.)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.